package pandora.apps.d3mob;

import java.util.ArrayList;

import pandora.apps.d3mob.controller.MainController;
import pandora.apps.d3mob.view.King;
import pandora.apps.d3mob.view.MapLoader;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Sprite;
import com.e3roid.drawable.modifier.AlphaModifier;
import com.e3roid.drawable.modifier.ProgressModifier;
import com.e3roid.drawable.modifier.SequenceModifier;
import com.e3roid.drawable.modifier.function.Linear;
import com.e3roid.drawable.texture.AssetTexture;
import com.e3roid.drawable.texture.TiledTexture;
import com.e3roid.drawable.tmx.TMXLayer;
import com.e3roid.drawable.tmx.TMXTiledMap;


public class MainLoop extends E3Activity {

	//SCENE
	private final static int WIDTH  = 320,
							HEIGHT = 480,
							HUD_XPOS = 0,
							HUD_YPOS = 0;
	private final static float
							HUD_WIDTH_PERC = 0.15f,
							HUD_HEIGHT_PERC = 1.00f,
							HUD_ALPHA = 0.4f;
	private E3Scene scene;

	//LOGO
	private static final String LOGO_FILE = "diablo3logo.png";
	private AssetTexture logoTexture;
	private Sprite logo;
	
	//MAP
	private static final String MAP_FILE = "desert.tmx";

	//KING
	private static final String KING_TEXTURE_FILE = "king.png";
	private TiledTexture kingTexture;
	private King king;
	
	//CONTROLLER
	//private static final String CONTROL_BASE_TEXTURE_FILE = "controller_base.png",
	//		CONTROL_KNOB_TEXTURE_FILE = "controller_knob.png";
	//private AssetTexture controlBaseTexture;
	//private AssetTexture controlKnobTexture;
	private MainController mainController;
	
	
	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestLandscape();
		engine.setRefreshMode(E3Engine.REFRESH_LIMITED);
		engine.setPreferredFPS(20);
		return engine;
	}

	@Override
	public void onLoadResources() {
		//LOGO
		this.logoTexture = new AssetTexture(LOGO_FILE, this);
		this.logoTexture.setReusable(true);
		int centerX = (getWidth()  - logoTexture.getWidth())  / 2;
		int centerY = (getHeight() - logoTexture.getHeight()) / 2;
		this.logo = new Sprite(logoTexture, centerX, centerY);
		
		//KING
		this.kingTexture = new TiledTexture(KING_TEXTURE_FILE, 31, 49, 0, 0, 3, 2, this);
		this.king = new King(this.kingTexture,100,100);
		
		//CONTROLLER
		//this.controlBaseTexture = new AssetTexture(CONTROL_BASE_TEXTURE_FILE, this);
		//this.controlKnobTexture = new AssetTexture(CONTROL_KNOB_TEXTURE_FILE, this);

		//MAP
		MapLoader loader = new MapLoader(MAP_FILE, this);
		loader.execute();		
	}

	@Override
	public E3Scene onLoadScene() {
		final E3Scene scene = new E3Scene();
		this.scene = scene;
		//BACKGROUND
		scene.setBackgroundColor(0f, 0f, 0f);

		//LOGO
		this.logo.addModifier(
				new SequenceModifier(
						new ProgressModifier(new AlphaModifier(0, 0, 1), 500, Linear.getInstance())//,
						//new DelayModifier(1000),
						//new ProgressModifier(new AlphaModifier(1, 1, 0), 500, Linear.getInstance())
				)
		);
		scene.getTopLayer().add(logo);
		return scene;
	}
	
	public void load(TMXTiledMap tmxMap) {
		if (tmxMap != null) {
			scene.getTopLayer().remove(logo);
			ArrayList<TMXLayer> mapLayers = tmxMap.getLayers();

			//CONTROLLER
			this.mainController = new MainController(HUD_XPOS,HUD_YPOS,Math.round(scene.getWidth()*HUD_WIDTH_PERC),Math.round(scene.getHeight()*HUD_HEIGHT_PERC));
			this.mainController.setScene(scene);
			this.mainController.setKing(king);
			this.mainController.setAlpha(HUD_ALPHA);
			scene.addHUD(mainController);
			
			if (mapLayers != null) {
				mainController.setLayers(mapLayers);
				for (TMXLayer layer : mapLayers) {
					layer.setSceneSize(getWidth(), getHeight());
					if ("Collision".equals(layer.getName())) {
						mainController.setCollisionLayer(layer);
						layer.addChild(king);
					} else {
						if ("Ground".equals(layer.getName())) {
							
						}
						//KING
						scene.getTopLayer().add(layer);
						scene.getTopLayer().add(king);
					}					
				}
				
			}
			//this.king.animate(StickController.DOWN);
			//engine.setRefreshMode(E3Engine.REFRESH_DEFAULT);
		}
	}

}
